# 小米笔试 - 防火墙问题
#主要分类：图论 + 并查集
# 1. 核心算法类型：
# 并查集 (Union-Find/Disjoint Set Union)
# 图论中的连通性问题


#题目描述：

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
    
    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    
    def union(self, x, y):
        px, py = self.find(x), self.find(y)
        if px == py:
            return
        if self.rank[px] < self.rank[py]:
            self.parent[px] = py
        elif self.rank[px] > self.rank[py]:
            self.parent[py] = px
        else:
            self.parent[py] = px
            self.rank[px] += 1
    
    def connected(self, x, y):
        return self.find(x) == self.find(y)

def solve():
    t = int(input())
    for _ in range(t):
        n, m = map(int, input().split())
        
        # 读取m对"不同类型"的攻击
        different_pairs = []
        line1 = list(map(int, input().split()))
        line2 = list(map(int, input().split()))
        
        for i in range(m):
            x, y = line1[i], line2[i]
            # 转换为0-based索引
            different_pairs.append((x-1, y-1))
        
        # 使用并查集维护"相同类型"关系
        uf = UnionFind(n)
        
        # 构建"相同类型"关系
        # 如果两个攻击不在different_pairs中，说明它们类型相同
        same_pairs = []
        for i in range(n):
            for j in range(i+1, n):
                if (i, j) not in different_pairs and (j, i) not in different_pairs:
                    same_pairs.append((i, j))
        
        # 将相同类型的攻击合并
        for x, y in same_pairs:
            uf.union(x, y)
        
        # 检查是否有矛盾
        # 如果两个攻击被标记为"不同类型"，但它们实际上属于同一个连通分量，则矛盾
        valid = True
        for x, y in different_pairs:
            if uf.connected(x, y):
                valid = False
                break
        
        print("Yes" if valid else "No")

if __name__ == "__main__":
    solve()